<script setup>
import { artAddChannelService, artEditChannelService, artGetChannelsService } from '@/api/article'
import { ref } from 'vue'
const dialogVisible = ref(false)
//提供el表单数据对象
const formModel = ref({
  cate_name: '',
  cate_alias: '',
})
const formRef = ref()

const open = (row) => {
  formModel.value = { ...row }
  dialogVisible.value = true
}
const emit = defineEmits(['success'])
const onSubmit = async () => {
  await formRef.value.validate()
  if (formModel.value.id) {
    await artEditChannelService(formModel.value)
    dialogVisible.value = false
    //再次渲染
    emit('success')
    ElMessage.success('编辑成功')
  } else {
    await artAddChannelService(formModel.value)
    dialogVisible.value = false
    //再次渲染
    emit('success')
    ElMessage.success('添加成功')
  }
}
defineExpose({
  open,
})

const rules = {
  cate_name: [
    { required: true, message: '分类名称不能为空', trigger: 'blur' },
    { pattern: /^\S{1,10}$/, message: '分类名称应该是1-10为非空字符', trigger: 'blur' },
  ],
  cate_alias: [
    {
      required: true,
      message: '分类别名不能为空',
      trigger: 'blur',
    },
    {
      pattern: /^[a-zA-Z0-9]{1,10}$/,
      message: '分类别名应该是1-10为非空字母或数字',
      trigger: 'blur',
    },
  ],
}
</script>
<template>
  <el-dialog v-model="dialogVisible" :title="formModel.id ? '编辑分类' : '添加分类'" width="500">
    <el-form :model="formModel" :rules="rules" ref="formRef">
      <el-form-item label="分类名称" prop="cate_name">
        <el-input v-model="formModel.cate_name" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="分类别名" prop="cate_alias">
        <el-input v-model="formModel.cate_alias" placeholder="请输入分类别名"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
